//
//  CalculatorViewController.swift
//  Calculator
//
//  Created by YJH on 2025/5/3.
//

import UIKit
import SnapKit

/// 1. button
/// 2. for 循环
/// 3. 用模型model的方法来表达view
/// 4. 把view的声明和使用放在CalculaltorView里面
/// 5. 代码规范

class CalculatorViewController: UIViewController {

    let calculator = Calculator()
    // 用于存储输入的内容
    private var inputString: String = ""
    // 用于显示点击按钮后的内容
    private lazy var displayLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.font = UIFont.systemFont(ofSize: 30, weight: .regular)
        label.textColor = .black
//        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    private lazy var calculatorView: CalculatorView = {
        let view = CalculatorView()
        for subview in view.subviews {
            if let button = subview as? UIButton {
                button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
            }
        }
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setupViews()
    }
    
    /// 设置视图
    private func setupViews() {
        // 添加显示标签到视图
        view.addSubview(displayLabel)
        displayLabel.snp.makeConstraints { make in
            make.top.equalTo(view.snp.top).offset(20)
            make.left.equalTo(view.snp.left).offset(20)
            make.right.equalTo(view.snp.right).inset(20)
            make.height.equalTo(50)
        }
//        NSLayoutConstraint.activate([
//            displayLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
//            displayLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
//            displayLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
//            displayLabel.heightAnchor.constraint(equalToConstant: 50)
//        ])
        
        view.addSubview(calculatorView)
        calculatorView.snp.makeConstraints { make in
            make.top.bottom.left.right.equalToSuperview()
        }
    }
    


//            let button = UIButton(type: .custom)
//            button.setTitle("1", for: .normal)
//            button.backgroundColor = .red
//            button.layer.borderColor = UIColor.green.cgColor
//            button.layer.borderWidth = 1
//            button.setTitleColor(.green, for: .normal)
//            view.addSubview(button)
//            button.snp.makeConstraints { make in
//                make.center.equalToSuperview()
            //        }
    
    func handleAC() {
        inputString = ""
        displayLabel.text = inputString
    }
    

    func delete() {
        if !inputString.isEmpty {
            inputString.removeLast()
            displayLabel.text = inputString
        }
    }
    
    func operation() {
        let operators: [Character] = ["+", "-", "x", "%","/"]
        for op in operators {
            if let index = inputString.firstIndex(of: op) {
                let leftStr = inputString[..<index]
                let rightStr = inputString[inputString.index(after: index)...]
                if let leftNum = Double(leftStr), let rightNum = Double(rightStr) {
                    var result: Double = 0
                    switch op {
                    case "+":
                        result = leftNum + rightNum
                    case "-":
                        result = leftNum - rightNum
                    case "x":
                        result = leftNum * rightNum
                    case "/":
                        result = leftNum / rightNum
                    case "%":
                        result = leftNum.truncatingRemainder(dividingBy: rightNum)
                    default:
                        break
                    }
                    inputString = String(result)
                    displayLabel.text = inputString
                    return
                }
            }
        }
    }
    
    /// 处理按钮点击
    /// - Parameter sender: 按钮
    @objc func buttonTapped(_ sender: UIButton) {
        guard let title = sender.title(for: .normal) else { return }
        switch title {
        case "AC":
            handleAC()
        case "Del":
            delete()
        case "=":
            operation()
        default:
            inputString += title
            displayLabel.text = inputString
        }
    }
//    @objc private func buttonTapped(_ sender: UIButton) {
//        if let title = sender.title(for: .normal) {
//            inputString.append(title)
//            displayLabel.text = inputString
//        }
//    }
}


